package com.ggh.jinjilive.ui.mine;

import android.os.Bundle;

import androidx.recyclerview.widget.LinearLayoutManager;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.blankj.utilcode.util.BarUtils;
import com.ggh.jinjilive.R;
import com.ggh.jinjilive.databinding.ActivityDiamondBinding;
import com.ggh.jinjilive.listener.AddItemOnClickListener;
import com.ggh.jinjilive.listener.AddOnClickListener1;
import com.ggh.jinjilive.mobile.CommonMobile;
import com.ggh.jinjilive.mobile.Pay.WeChatPayMobile;
import com.ggh.jinjilive.mobile.mine.DiamondMobile;
import com.ggh.jinjilive.ui.BaseActivity;
import com.ggh.jinjilive.ui.mine.adapter.DiamondAdapter;
import com.ggh.jinjilive.utils.ARouterUtils;
import com.ggh.jinjilive.utils.GeneralUtils;
import com.ggh.jinjilive.utils.pay.PayUtils;
import com.gyf.immersionbar.ImmersionBar;

import java.util.HashMap;
import java.util.Map;

/**
 * 钻石
 */
@Route(path = ARouterUtils.DiamondActivity)
public class DiamondActivity extends BaseActivity<ActivityDiamondBinding> implements AddItemOnClickListener {
    //适配器
    private DiamondAdapter diamondAdapter;
    @Override
    public void initCreate() {
        super.initCreate();
        ImmersionBar.with(this).keyboardEnable(true).init();
    }

    @Override
    public int initContentView(Bundle savedInstanceState) {
        return R.layout.activity_diamond;
    }


    @Override
    public void initView() {
        super.initView();
        setWhiteActionBarStyle("我的钻石",true,true);
        //获取状态栏高度
        binding.llSpacing.setPadding(0, BarUtils.getStatusBarHeight(),0,0);
    }

    @Override
    public void initData() {
        super.initData();
        diamondAdapter = new DiamondAdapter();
        diamondAdapter.setAddItemOnClickListener(this);
        binding.mineMydiamondRecyclerview.setLayoutManager(new LinearLayoutManager(mContext));
        binding.mineMydiamondRecyclerview.setAdapter(diamondAdapter);
    }

    @Override
    public void initViewObservable() {
        super.initViewObservable();
        //获取钻石数量和数据列表
        Map<String, Object> params = new HashMap<>();
        GeneralUtils.authBuylist(params, new AddOnClickListener1() {
            @Override
            public void onSkipClicked(Object mObject) {
                CommonMobile commonMobile = (CommonMobile) mObject;
                if(null != commonMobile){
                    //钻石
                    binding.myDiamondNum.setText(commonMobile.getMy_diamond());
                    //充值列表
                    diamondAdapter.setAdd(commonMobile.getList());
                }
            }
        });
    }

    @Override
    public void onSkipClicked(int position, Object mObject) {
        DiamondMobile diamondMobile = (DiamondMobile) mObject;
        if(null != diamondMobile){
            if(null != payChooseDialog && !payChooseDialog.isShowing()) {
                payChooseDialog.show();
                payChooseDialog.setPayMoneyContet(diamondMobile.getPrice() +"");
                payChooseDialog.setAddItemOnClickListener(new AddItemOnClickListener() {
                    @Override
                    public void onSkipClicked(int position, Object mObject) {
                        payChooseDialog.dismiss();
                        WeChatAlipay = position;
                        Map<String, Object> params = new HashMap<>();
                        params.put("diamond_id",diamondMobile.getId());
                        GeneralUtils.myBuyDiamond(params, new AddOnClickListener1() {
                            @Override
                            public void onSkipClicked(Object mObject) {
                                CommonMobile commonMobile = (CommonMobile) mObject;
                                if(null != commonMobile){
                                    Map<String, Object> params = new HashMap<>();
                                    params.put("order_sn", commonMobile.getOrder_sn());
                                    if(WeChatAlipay == 0){//微信
                                        params.put("pay_type","wechat");
                                        GeneralUtils.WeChatMyPay(params, new AddOnClickListener1() {
                                            @Override
                                            public void onSkipClicked(Object mObject) {
                                                WeChatPayMobile weChatPayMobile = (WeChatPayMobile) mObject;
                                                if(null != weChatPayMobile){
                                                    //调起微信
                                                    PayUtils.getInstance(mContext).WeChatPay(weChatPayMobile);
                                                }
                                            }
                                        });
                                    }else if(WeChatAlipay == 1){//支付宝
                                        params.put("pay_type","alipay");
                                        GeneralUtils.alipayMyPay(params, new AddOnClickListener1() {
                                            @Override
                                            public void onSkipClicked(Object mObject) {
                                                String AlipayPay = (String) mObject;
                                                //调起支付宝
                                                PayUtils.getInstance(mContext).AlipayPay(AlipayPay);
                                            }
                                        });
                                    }
                                }
                            }
                        });
                    }
                });
            }
        }
    }
}
